package com.tiancheng.onecode.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.model.response.api.QueryDeviceListRes;
import com.tiancheng.onecode.model.response.api.QueryDeviceTypeRes;
import com.tiancheng.onecode.model.thrid.AddDeviceReq;
import com.tiancheng.onecode.model.thrid.DeviceNoticeReq;
import com.tiancheng.onecode.trade.utils.ForestHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: kellen
 * @Description: 设备管理中心-工具类
 * @Date: create in 2025/2/27 09:57
 */
@Slf4j
@Component
public class DeviceCenterUtils {

    @Value("${device-center.host}")
    private String deviceCenterHost;

    /**
     * 查询设备类型
     *
     * @return List<QueryDeviceTypeRes>
     */
    public List<QueryDeviceTypeRes> queryDeviceType() {
        final String s = ForestHttpUtil.getInstance().doPostJson(deviceCenterHost + "/GetSpeakerDeviceType", null, null);
        log.info("调用云音响查询设备类型返回：{}", s);
        Asserts.isNotEmpty(s, "查询设备类型失败！");
        JSONObject jsonObject = JsonUtil.fromJson(s, JSONObject.class);
        Asserts.isTrue(null != jsonObject && jsonObject.containsKey("Success") && jsonObject.getBoolean("Success"), "查询设备类型失败！");
        JSONObject types = jsonObject.getJSONObject("Data");
        List<QueryDeviceTypeRes> result = new ArrayList<QueryDeviceTypeRes>();
        for (String key : types.keySet()) {
            result.add(new QueryDeviceTypeRes(key, types.getString(key)));
        }
        return result;
    }


    /**
     * 查询设备列表
     *
     * @return List<QueryDeviceTypeRes>
     */
    public List<QueryDeviceListRes> queryDeviceList(Long platformMerchantId) {
        Map<String, String> params = new HashMap<>();
        params.put("BusinessUnitID",platformMerchantId.toString());
        final String s = ForestHttpUtil.getInstance().doPostJson(deviceCenterHost + "/GetDeviceList", params, null);
        log.info("调用云音响查询设备类型返回：{}", s);
        Asserts.isNotEmpty(s, "查询设备类型失败！");
        JSONObject jsonObject = JsonUtil.fromJson(s, JSONObject.class);
        Asserts.isTrue(null != jsonObject && jsonObject.containsKey("Success") && jsonObject.getBoolean("Success"), "查询设备类型失败！");
        JSONObject devicesObj = jsonObject.getJSONObject("DSet");
        if(null==devicesObj){
            return Collections.emptyList();
        }
        JSONArray devicesJsonArray = devicesObj.getJSONArray("Table1");
        if(null==devicesJsonArray ||devicesJsonArray.size()==0){
            return Collections.emptyList();
        }
        List<QueryDeviceListRes> devices = new ArrayList<>();
        for (Object d : devicesJsonArray) {
            devices.add(JsonUtil.fromJson(JsonUtil.toJsonString(d), QueryDeviceListRes.class));
        }
        return devices;
    }


    /**
     * 添加设备
     *
     * @param addDeviceReq 添加设备请求参数
     * @return id
     */
    public void addDevice(AddDeviceReq addDeviceReq) {
        final String s = ForestHttpUtil.getInstance().doPostJson(deviceCenterHost + "/AddDevice", JsonUtil.toJsonString(addDeviceReq), null);
        log.info("调用云音响添加设备返回：{}", s);
        Asserts.isNotEmpty(s, "添加设备失败！");
        JSONObject jsonObject = JsonUtil.fromJson(s, JSONObject.class);
        Asserts.isTrue(null != jsonObject && jsonObject.containsKey("Success") && jsonObject.getBoolean("Success"), "添加设备失败！");
    }


    /**
     * 查询设备类型
     *
     * @return List<QueryDeviceTypeRes>
     */
    public void removeDevice(String id) {
        Map<String, String> params = new HashMap<>();
        params.put("ID", id);
        final String s = ForestHttpUtil.getInstance().doPostJson(deviceCenterHost + "/DeleteDevice", params, null);
        log.info("调用云音响移除设备返回：{}", s);
        Asserts.isNotEmpty(s, "移除设备失败！");
        JSONObject jsonObject = JsonUtil.fromJson(s, JSONObject.class);
        Asserts.isTrue(null != jsonObject && jsonObject.containsKey("Success") && jsonObject.getBoolean("Success"), "移除设备失败！");
    }

    /**
     * 语音播报
     */
    public void notice(DeviceNoticeReq deviceNoticeReq) {
        deviceNoticeReq.setSign(deviceNoticeReq.genSign());
        final String s = ForestHttpUtil.getInstance().doPostJson(deviceCenterHost + "/CloudBroadcast", JsonUtil.toJsonString(deviceNoticeReq), null);
        log.info("调用云音响消息推送返回：{}", s);
        Asserts.isNotEmpty(s, "消息推送到云音响失败！");
        JSONObject jsonObject = JsonUtil.fromJson(s, JSONObject.class);
        Asserts.isTrue(null != jsonObject && jsonObject.containsKey("Success") && jsonObject.getBoolean("Success"), "消息推送到云音响失败！");
    }

}
